package com.example.port.Controller;

import com.example.port.dto.ApiResponse;
import com.example.port.entity.StorageFee;
import com.example.port.service.StorageFeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/storage-fees")
public class StorageFeeController {
    
    private final StorageFeeService storageFeeService;
    
    @Autowired
    public StorageFeeController(StorageFeeService storageFeeService) {
        this.storageFeeService = storageFeeService;
    }
    
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public ApiResponse<Integer> createStorageFee(@RequestBody StorageFee fee) {
        int result = storageFeeService.insertStorageFee(fee);
        if (result > 0) {
            return ApiResponse.success(result);
        }
        return ApiResponse.error(500, "创建仓储费用失败");
    }
    
    @GetMapping
    public ApiResponse<List<StorageFee>> getAllStorageFees() {
        List<StorageFee> storageFees = storageFeeService.getAllStorageFees();
        return ApiResponse.success(storageFees);
    }
    
    @GetMapping("/{feeId}")
    public ApiResponse<StorageFee> getStorageFeeById(@PathVariable Long feeId) {
        StorageFee fee = storageFeeService.getStorageFeeById(feeId);
        if (fee != null) {
            return ApiResponse.success(fee);
        }
        return ApiResponse.error(404, "未找到指定的仓储费用");
    }
    
    @PutMapping("/{feeId}/pay")
    public ApiResponse<Integer> payStorageFee(@PathVariable Long feeId) {
        int result = storageFeeService.payStorageFee(feeId);
        if (result > 0) {
            return ApiResponse.success(result);
        }
        return ApiResponse.error(500, "支付仓储费用失败");
    }
    
    @DeleteMapping("/{feeId}")
    public ApiResponse<Integer> deleteStorageFee(@PathVariable Long feeId) {
        int result = storageFeeService.deleteStorageFee(feeId);
        if (result > 0) {
            return ApiResponse.success(result);
        }
        return ApiResponse.error(500, "删除仓储费用失败");
    }
} 
